package io.spring.guides.uploading_files;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@Controller
public class FileUploadController {
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public
@ResponseBody
String provideUploadInfo() {
return "You can upload a file by posting to this same URL.";
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public
@ResponseBody
String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)))) {
stream.write(file.getBytes());
}
return "You successfully uploaded " + name + "!";
} catch (IOException e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}